package org.kanstantin.irrp;


import com.meterware.httpunit.*;
import com.sun.org.apache.xpath.internal.XPathAPI;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sun.util.LocaleServiceProviderPool;

import java.io.*;
import java.util.logging.Logger;

public class PrimitiveCrawler {
    private static final Logger LOG = Logger.getLogger(PrimitiveCrawler.class.getName());

    WebConversation conversation;

    public PrimitiveCrawler() {
        conversation = new WebConversation();
        HttpUnitOptions.setScriptingEnabled(false);
    }

    public void process() throws Exception {
        WebRequest req = new GetMethodWebRequest("http://irr.tut.by/searchads/electronicsandtechnics/appliance/search/keywords=%D1%85%D0%BE%D0%BB%D0%BE%D0%B4%D0%B8%D0%BB%D1%8C%D0%BD%D0%B8%D0%BA%D0%B8/tab=users/page6/");
        WebResponse resp = conversation.getResponse(req);
            int x = 0;
            for (WebLink link : resp.getLinks()) {
                System.out.println(++x + link.getText());
            }
        String xpath = "/HTML/BODY/DIV[9]/DIV/DIV/DIV/DIV[2]/DIV[2]/DIV[3]/TABLE/TBODY/TR";
        NodeList nodeList = XPathAPI.selectNodeList(resp.getDOM(), xpath);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            System.out.println(node.getTextContent());
        }
        int b = 1;
    }

//            BufferedReader reader = null;
//            try {
//                InputStream is = resp.getInputStream();
//                reader = new BufferedReader(new InputStreamReader(is));
//            } catch (IOException ex) {
//                LOG.severe(ex.getMessage());
//            }
//            String s = null;
//            try {
//                while ((s = reader.readLine()) != null) {
//                    System.out.println(s);
//                }
//
//            } catch (IOException ex) {
//                LOG.severe(ex.getMessage());
//            }

}
